package com.aiti.lulian.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.CardZfFloorTextDto;
import com.aiti.lulian.entity.crad.CardZfSort;
import com.aiti.lulian.entity.crad.cluster.CardZfFloorText;
import com.aiti.lulian.mapper.card.cluster.CardZfFloorTextMapper;
import com.aiti.lulian.mapper.card.cluster.CardZfSortMapper;
import com.aiti.lulian.service.ICardZfFloorTextService;
import com.aiti.lulian.service.ICardZfSortService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CardZfFloorTextServiceImpl extends BaseSerivceImpl<CardZfFloorTextMapper, CardZfFloorText> implements ICardZfFloorTextService {

    private final CardZfFloorTextMapper baseMapper;

    private final CardZfSortMapper cardZfSortMapper;

    private final ICardZfSortService cardZfSortService;

    public CardZfFloorTextServiceImpl(CardZfFloorTextMapper baseMapper,CardZfSortMapper cardZfSortMapper,ICardZfSortService cardZfSortService) {
        this.baseMapper = baseMapper;
        this.cardZfSortMapper = cardZfSortMapper;
        this.cardZfSortService = cardZfSortService;
    }

    @Override
    public List<CardZfFloorText> selectZfFloorListByUserId(String enterpriseId,String flag) {
        QueryWrapper<CardZfFloorText> qw = new QueryWrapper<>();
        qw.eq("enterprise_id", enterpriseId);
        if(StringUtils.equals(flag,"wechat")){
            qw.eq("is_show","0");
        }
        qw.orderByAsc("sort");
        return baseMapper.selectList(qw);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteZfFloorText(CardZfFloorText zfFloorText) {
        baseMapper.deleteById(zfFloorText.getBaseId());
        cardZfSortMapper.delete(new QueryWrapper<CardZfSort>().eq("ys_id", zfFloorText.getBaseId()));
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public Message saveOrUpdateCardZfFloorText(CardZfFloorTextDto cardZfFloorTextDto,String enterpriseId) {

        List<CardZfFloorText> cardZfFloorTexts = cardZfFloorTextDto.getCardZfFloorTexts();
        if(cardZfFloorTexts != null && !cardZfFloorTexts.isEmpty()){
            for(CardZfFloorText temp : cardZfFloorTexts){
                temp.setUserId(cardZfFloorTextDto.getUserId());
                temp.setEnterpriseId(enterpriseId);
            }
            List<CardZfSort> addSortDataList = new ArrayList<>();
            List<CardZfSort> updateSortDataList = new ArrayList<>();
            List<CardZfFloorText> collect = cardZfFloorTexts.stream().filter(item -> StringUtils.isBlank(item.getBaseId())).collect(Collectors.toList());
            List<CardZfFloorText> updateData = cardZfFloorTexts.stream().filter(item -> StringUtils.isNotBlank(item.getBaseId())).collect(Collectors.toList());

            for(CardZfFloorText addDate : collect){
                addDate.setBaseId(IdUtil.simpleUUID());
                CardZfSort ppp = new CardZfSort();
                ppp.setEnterpriseId(enterpriseId);
                ppp.setYsId(addDate.getBaseId());
                ppp.setContent(addDate.getContent());
                ppp.setTitle(addDate.getTitle());
                ppp.setId(IdUtil.simpleUUID());
                ppp.setBaseCreateBy(cardZfFloorTextDto.getUserId());
                ppp.setBaseCreateTime(DateUtil.date());
                ppp.setType("lc");
                addSortDataList.add(ppp);
            }
            if(collect != null && !collect.isEmpty()){
                this.saveBatch(collect);
            }

            if(updateData != null && !updateData.isEmpty()){
                this.updateBatchById(updateData);
                for(CardZfFloorText temp : updateData){
                    CardZfSort ppp = new CardZfSort();
                    ppp.setYsId(temp.getBaseId());
                    ppp.setContent(temp.getContent());
                    ppp.setTitle(temp.getTitle());
                    ppp.setId(IdUtil.simpleUUID());
                    ppp.setBaseCreateBy(cardZfFloorTextDto.getUserId());
                    ppp.setBaseCreateTime(DateUtil.date());
                    ppp.setType("lc");
                    updateSortDataList.add(ppp);
                }
            }
            if(!updateSortDataList.isEmpty()){
                cardZfSortMapper.customUpdate(updateSortDataList);
            }
            if(!addSortDataList.isEmpty()){
                cardZfSortService.saveBatch(addSortDataList);
            }
        }
        return Message.success();
//        LambdaQueryWrapper<CardZfFloorText> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(CardZfFloorText::getUserId, cardZfFloorTextDto.getUserId());
//        this.remove(wrapper);
//
//        List<CardZfFloorText> collect = cardZfFloorTextDto.getCardZfFloorTexts().stream().peek(i -> i.setUserId(cardZfFloorTextDto.getUserId())).collect(Collectors.toList());
//        boolean saveBatch = this.saveBatch(collect);
//
//        if (saveBatch) {
//            return Message.success("操作成功");
//        } else {
//            return Message.fail("操作失败");
//        }
    }
}